package com.wodogs.utils.tools;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.CookieStore;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.cookie.BasicClientCookie;

public class Test {

	public static void main(String[] args) {
//		try {
//			List<String> list = new ArrayList<>();
//			for (int i = 0; i < 100; i++) {
//				list.add(i + ",");
//			}
//			
//			System.out.println(new Test().list2Str(list, 5));
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		
//		String str = "fts=1448611348;CNZZDATA2724999=cnzz_eid=88524096-1448611345-&ntime=1448611345;pgv_pvi=2163233792;DedeUserID__ckMd5=ccc4ae1f461f37f2;sid=axmq42gd;_cnt_notify=0;LIVE_LOGIN_DATA=26a54a8ad6751b56dd2eab8a85869a4c1f1f4277;DedeUserID=18475947;_cnt_pm=0;pgv_si=s1597879296;_cnt_dyn=null;uTZ=-480;_dfcaptcha=01a031a21d1726f86dcb0c131324b41c;LIVE_LOGIN_DATA__ckMd5=0186bdeb97b94980;SESSDATA=eb644914,1449216140,6af2eb37;";
//		new Test().Str2Cookie(str);
		System.out.println(String.format("aaa%s_%d", 1,1));
	}
	
	public CookieStore Str2Cookie(String str){
		CookieStore cookieStore = new BasicCookieStore();
		if(StringUtils.isBlank(str)){
			return cookieStore;
		}
		String[]ary = str.split(";");
		for(String c:ary){
			String key = c.substring(0, c.indexOf("="));
			String value = c.substring(c.indexOf("=")+1);
			BasicClientCookie cookie = new BasicClientCookie(key,value);
			cookieStore.addCookie(cookie);
		}
		
		return cookieStore;
	}

	public String list2Str(List<String> list, final int nThreads) throws Exception {
		if (list == null || list.isEmpty()) {
			return null;
		}
		
		StringBuffer ret = new StringBuffer();

		int size = list.size();
		ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
		List<Future<String>> futures = new ArrayList<Future<String>>(nThreads);
		
		for (int i = 0; i < nThreads; i++) {
			final List<String> subList = list.subList(size / nThreads * i, size / nThreads * (i + 1));
			Callable<String> task = new Callable<String>() {
				@Override
				public String call() throws Exception {
					StringBuffer sb = new StringBuffer();
					for (String str : subList) {
						sb.append(str);
					}
					return sb.toString();
				}
			};
			futures.add(executorService.submit(task));
		}
		
		for (Future<String> future : futures) {
			ret.append(future.get());
		}
		executorService.shutdown();
		
		return ret.toString();
	}
}
